package secretary;

import java.sql.SQLException;

import javax.swing.JFrame;

import secretaryGUI.createSecretaryWindow;
import secretaryGUI.secretaryAlerts;

import basic.ClientConsole;

import com.sun.rowset.CachedRowSetImpl;

import flexibility.userData;
import gui.secretaryScreen;
import guiCode.secretaryScreenCode;
//this class will create new classes in the database by user request
public class createNewClass {
	private int semesterID;
	secretaryAlerts sa = new secretaryAlerts();
	public void createNewClass(createSecretaryWindow csw, secretaryScreenCode ssc,JFrame secretaryScreen,String className, ClientConsole secretaryConnection,CachedRowSetImpl crs, userData secretaryData, secretaryScreen ss, int semester) {
		
		switch(semester){
		case 1://this is a case for the next semester planning
			
			//now we will find the next semester id number, the next semester is recognized from the endate of 2999
			secretaryData.deleteAll();// this will get the semester with highest ID
			secretaryData.addData(18);
			secretaryData.addData(1);
			secretaryConnection.acceptMsg(secretaryData);
			crs = secretaryConnection.getResults();
			try {
				crs.next();
				//this will put the semester id number into semester variable
				semesterID = Integer.parseInt(crs.getString(1));
			}catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			secretaryData.deleteAll();
			secretaryData.addData(13);// check if this class already exists
			secretaryData.addData(1);
			secretaryData.addData(className);
			secretaryData.addData(semesterID);
			secretaryConnection.acceptMsg(secretaryData);
			crs = secretaryConnection.getResults();
			try {
				if (crs.next() == false) {
					// class can be created
					secretaryData.deleteAll();
					secretaryData.addData(14);
					secretaryData.addData(0);
					secretaryData.addData(semesterID);
					secretaryData.addData(className);
					secretaryConnection.acceptMsg(secretaryData);
					crs = secretaryConnection.getResults();
					sa.showMessages(csw, ssc, secretaryScreen, "class " + className + " was created",
							"Class Editor - confirmation", 4);
				} else {
					sa.showMessages(csw, ssc, secretaryScreen,
							"There is already a class with this class name",
							"Class editor failure", 1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case 0://this is a case for the current semester editing
			//now we will find the next semester id number, the next semester is recognized from the endate of 2999
			secretaryData.deleteAll();// this will get the semester with highest ID
			secretaryData.addData(17);
			secretaryData.addData(1);
			secretaryConnection.acceptMsg(secretaryData);
			crs = secretaryConnection.getResults();
			try {
				crs.next();
				//this will put the semester id number into semester variable
				semesterID = Integer.parseInt(crs.getString(1));
			}catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			secretaryData.deleteAll();
			secretaryData.addData(13);// check if this class already exists
			secretaryData.addData(1);
			secretaryData.addData(className);
			secretaryData.addData(semesterID);
			secretaryConnection.acceptMsg(secretaryData);
			crs = secretaryConnection.getResults();
			try {
				if (crs.next() == false) {
					// class can be created
					secretaryData.deleteAll();
					secretaryData.addData(14);
					secretaryData.addData(0);
					secretaryData.addData(semesterID);
					secretaryData.addData(className);
					secretaryConnection.acceptMsg(secretaryData);
					crs = secretaryConnection.getResults();
					sa.showMessages(csw, ssc, secretaryScreen,"class " + className + " was created",
							"Class Editor - confirmation", 4);
				} else {
					sa.showMessages(csw, ssc, secretaryScreen,
							"There is already a class with this class name",
							"Class editor failure", 1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		}
		
		
	}
}
